package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.RoleDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.pojo.Role;
import com.itheima.health.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = RoleService.class)
@Transactional
public class RoleServiceImpl implements RoleService {
    @Autowired
    RoleDao roleDao;

    @Override
    public void add(Role role, Integer[] permissionIds, Integer[] menuIds) {
        roleDao.add(role);
        if (permissionIds != null && permissionIds.length > 0) {
            for (Integer permissionId : permissionIds) {
                Map<String, Integer> map = new HashMap<>();
                map.put("role_id", role.getId());
                map.put("permission_id", permissionId);
                roleDao.addRolePermission(map);
            }
        }
        if (menuIds != null && menuIds.length > 0) {
            for (Integer menuId : menuIds) {
                Map<String, Integer> map = new HashMap<>();
                map.put("role_id", role.getId());
                map.put("menu_id", menuId);
                roleDao.addRoleMenu(map);
            }
        }
    }

    @Override
    public void delete(Integer id) {
        List<Integer> role1 = roleDao.findRoleInUserRole(id);
        if (role1.size()>0) {
            throw new RuntimeException("当前已被用户角色表关联，不能进行删除");
        }
        List<Integer> role2 = roleDao.findRoleInRolePermission(id);
        if (role2.size()>0) {
            throw new RuntimeException("当前已被角色权限表关联，不能进行删除");
        }
        List<Integer> role3 = roleDao.findRoleInRoleMenu(id);
        if (role3.size()>0) {
            throw new RuntimeException("当前已被角色菜单表关联，不能进行删除");
        }
        roleDao.delete(id);
    }

    @Override
    public void edit(Role role, Integer[] permissionIds, Integer[] menuIds) {
        //修改role表
        roleDao.deleteForEditWithUser(role.getId());
        roleDao.deleteForEditWithPermission(role.getId());
        if (permissionIds != null) {
            for (Integer permissionId : permissionIds) {
                Map<String, Object> map = new HashMap<>();
                map.put("role_id", role.getId());
                map.put("permission_id", permissionId);
                roleDao.editInPermission(map);
            }
        }
        roleDao.deleteForEditWithMenu(role.getId());
        if (menuIds != null) {
            for (Integer menuId : menuIds) {
                Map<String, Object> map = new HashMap<>();
                map.put("role_id", role.getId());
                map.put("menu_id", menuId);
                roleDao.editInMenu(map);
            }
        }
    }

    @Override
    public List<Role> findAll() {
        return roleDao.findAll();
    }

    @Override
    public Role findById(Integer id) {
        return roleDao.findById(id);
    }

    @Override
    public List<Integer> findMenuByRoleId(Integer id) {
        return roleDao.findMenuByRoleId(id);
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Page<Role> page = roleDao.findPage(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public List<Integer> findPermissionByRoleId(Integer id) {
        return roleDao.findPermissionByRoleId(id);
    }

}
